<template>
  <div
    ref="navigationRef"
    class="pc-navigation bg-white dark:bg-zinc-800 duration-500"
  >
    <ul class="navigation-group" id="media" :style="groupStyle">
      <li
        class="navigation-item dark:text-zinc-500 dark:hover:text-zinc-300"
        :class="{ active: index === categoryStore.index }"
        v-for="(item, index) in data"
        :key="item.id"
        @click="clickItem(item, index)"
      >
        <span class="item-name"> {{ item.name }} </span>
      </li>
      <li class="control" @click="handleStretch">
        <img
          class="dark:bg-white dark:rounded-sm"
          v-show="!isStretch"
          src="@/assets/style/svg/arrow-top.svg"
        />
        <img
          class="dark:bg-white dark:rounded-sm"
          v-show="isStretch"
          src="@/assets/style/svg/arrow-down.svg"
        />
      </li>
    </ul>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useCategoryStore } from '@/store/category'
import { onClickOutside } from '@vueuse/core'

defineProps({
  data: Array
})

const navigationRef = ref(null)
const categoryStore = useCategoryStore()

// 控制高度
const isStretch = ref(true)
const handleStretch = () => {
  isStretch.value = !isStretch.value
}

// group样式
const groupStyle = computed(() => ({
  height: isStretch.value ? '50px' : '206px'
}))

// 点击 li 标签
// 索引
const clickItem = (item, index) => {
  categoryStore.currentCategory = item
  categoryStore.index = index
}

/**
 * 点击区域外 收起导航栏
 */
onClickOutside(navigationRef, () => {
  isStretch.value = true
})
</script>

<style lang="scss" scoped>
.pc-navigation {
  position: sticky;
  top: 0;
  z-index: 11;
  padding: 10px 0;
  .navigation-group {
    position: relative;
    margin: 0 auto;
    padding: 10px;
    box-sizing: border-box;
    width: 800px;
    display: flex;
    align-content: flex-start;
    justify-content: center;
    flex-wrap: wrap;
    overflow: hidden;
    transition: all 0.3s;
    .navigation-item {
      flex-shrink: 0;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      margin: 0 16px;
      font-size: 0.16rem;
      border-radius: 10px;
      cursor: pointer;
      transition: all 0.2s;
      &.active {
        background-color: $primary-background-color;
        color: $light-text-color;
      }
      .item-name {
        font-weight: 700;
        font-size: 0.16rem;
        line-height: 30px;
        padding: 5px;
      }
    }
    .control {
      width: 20px;
      height: 20px;
      position: absolute;
      right: 5px;
      bottom: 0px;
      font-weight: 700;
      font-size: 0.16rem;
      cursor: pointer;
      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}
</style>
